import {defineComponent, h} from "vue";
import {Toast} from "vant";
import {showComponentToBody} from "@/utils";

type NotifyConfig = {
  title: string;
  message?: string;
  type: 'confirm' | 'success' | 'info' | 'error' | 'toast';
  confirm?: Function,
  cancel?: Function,
}

export default function (config: NotifyConfig) {
  showComponentToBody(h(Comp, {
    message: config.title
  }));
}

const Comp = defineComponent({
  props: {
    message: {
      type: String,
      default: 'undefined'
    },
    icon: {
      type: String,
      default: 'fail'
    },
    timeout: {
      type: Number,
      default: 2000
    }
  },
  mounted() {
    setTimeout(() => {
      this.$emit('close')
    }, this.timeout)
  },
  render() {
    return h(Toast, {
      show: true,
      ...this.$props
    }, {
      //@ts-ignore
      message: () => [
        this.message
      ]
    })
  },
});